home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / Paths ƒ / Paths.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-08  |  8.8 KB  |  343 lines  |  [TEXT/KAHL]

  1. /*******************************************************************************
  2.  * Paths.c
  3.  *
  4.  * See header file for detailed information.
  5.  *
  6.  *******************************************************************************/
  7.  
  8. #include "Paths.h"
  9. #include <Folders.h>
  10.  
  11.  
  12. // local prototypes
  13.  
  14. short Find_File1(CInfoPBRec *fInfo, Str32 findFile, long searchDirId, short searchIndex,
  15.                 short searchDepth, long *foundDirId, short *foundIndex, Boolean findDirs,
  16.                 Boolean findFiles, Boolean *foundDirFlag, Boolean *foundFileFlag);
  17. pascal short SFGetDirHook(short item, DialogPtr theDialog);
  18.  
  19.  
  20.  
  21. /*******************************************************************************
  22.  * Get_Root_Vol_Info
  23.  *
  24.  *******************************************************************************/
  25.  
  26. short Get_Root_Vol_Info(short *rootVolNum, Str32 rootVolName)
  27. {
  28.     ParamBlockRec    vInfo;
  29.     OSErr            err;
  30.     
  31.     rootVolName[0] = '\0';
  32.     vInfo.volumeParam.ioCompletion = NULL;
  33.     vInfo.volumeParam.ioVolIndex = 0;
  34.     vInfo.volumeParam.ioNamePtr = rootVolName;
  35.     vInfo.volumeParam.ioVRefNum = 0;
  36.     err = PBGetVInfo(&vInfo, FALSE);
  37.     if (rootVolNum)
  38.         *rootVolNum = vInfo.volumeParam.ioVRefNum;
  39.     
  40.     return err;
  41. } // Get_Root_Vol_Info
  42.  
  43.  
  44.  
  45. /*******************************************************************************
  46.  * Prepend_Dir_To_Path
  47.  *
  48.  *******************************************************************************/
  49.  
  50. void Prepend_Dir_To_Path(Str32 dirName, Str255 pathName, Str255 fullDirPath)
  51. {
  52.     short    hasPath = (pathName[0] != '\0');
  53.     
  54.     BlockMove((Ptr)(pathName + 1), (Ptr)(fullDirPath + 2 + dirName[0]), pathName[0]);
  55.     BlockMove((Ptr)(dirName + 1), (Ptr)(fullDirPath + 1), dirName[0]);
  56.     fullDirPath[dirName[0] + 1] = ':';
  57.     fullDirPath[0] = dirName[0] + pathName[0] + hasPath;
  58. } // Prepend_Dir_To_Path
  59.  
  60.  
  61.  
  62. /*******************************************************************************
  63.  * Append_File_To_Path
  64.  *
  65.  *******************************************************************************/
  66.  
  67. void Append_File_To_Path(Str32 fileName, Str255 pathName, Str255 fullFilePath)
  68. {
  69.     short    noColon = (pathName[pathName[0]] != ':');
  70.     
  71.     if (pathName[0] == '\0')
  72.         BlockMove((Ptr)fileName, (Ptr)fullFilePath, fileName[0] + 1);
  73.     else
  74.     {
  75.         if (pathName != fullFilePath)
  76.             BlockMove((Ptr)pathName, (Ptr)fullFilePath, pathName[0] + 1);
  77.         BlockMove((Ptr)(fileName + 1), (Ptr)(fullFilePath + pathName[0] + 1 + noColon), fileName[0]);
  78.         if (noColon)
  79.             fullFilePath[pathName[0] + 1] = ':';
  80.         fullFilePath[0] = pathName[0] + fileName[0] + noColon;
  81.     }
  82. } // Append_File_To_Path
  83.  
  84.  
  85.  
  86. /*******************************************************************************
  87.  * Last_File_In_Path
  88.  *
  89.  *******************************************************************************/
  90.  
  91. void Last_File_In_Path(Str255 fullFilePath, Str32 fileName)
  92. {
  93.     short        pos = fullFilePath[0];
  94.     
  95.     while (pos > 0)
  96.     {
  97.         if (fullFilePath[pos] == ':')
  98.             break;
  99.         pos--;
  100.     }
  101.     BlockMove((Ptr)(fullFilePath + pos + 1), (Ptr)(fileName + 1), fullFilePath[0] - pos);
  102.     fileName[0] = fullFilePath[0] - pos;
  103.     return;
  104. } // Last_File_In_Path
  105.  
  106.  
  107.  
  108. /*******************************************************************************
  109.  * Path_To_DirId
  110.  *
  111.  *******************************************************************************/
  112.  
  113. short Path_To_DirId(Str32 rootVolName, Str255 pathToDir, short *vRefNum, long *dirId)
  114. {
  115.     Str255            fullPath;
  116.     union
  117.     {
  118.         CInfoPBRec        dInfo;
  119.         ParamBlockRec    vInfo;
  120.     } pb;
  121.     OSErr            err;
  122.     
  123.     if (rootVolName[0] != '\0')
  124.         Prepend_Dir_To_Path(rootVolName, pathToDir, fullPath);
  125.     else
  126.         rootVolName = pathToDir;
  127.     
  128.     pb.vInfo.volumeParam.ioCompletion = NULL;
  129.     pb.vInfo.volumeParam.ioVolIndex = -1;
  130.     pb.vInfo.volumeParam.ioNamePtr = rootVolName;
  131.     pb.vInfo.volumeParam.ioVRefNum = 0;
  132.     err = PBGetVInfo(&pb.vInfo, FALSE);
  133.     if (err != noErr)
  134.         return err;
  135.     *vRefNum = pb.vInfo.volumeParam.ioVRefNum;
  136.  
  137.     if (pathToDir[0] == '\0')
  138.     {
  139.         fullPath[0]++;
  140.         fullPath[fullPath[0]] = ':';
  141.     }
  142.     
  143.     pb.dInfo.dirInfo.ioCompletion = NULL;
  144.     pb.dInfo.dirInfo.ioNamePtr = fullPath;
  145.     pb.dInfo.dirInfo.ioVRefNum = 0;
  146.     pb.dInfo.dirInfo.ioFDirIndex = 0;
  147.     pb.dInfo.dirInfo.ioDrDirID = 0L;
  148.     err = PBGetCatInfo(&pb.dInfo, FALSE);
  149.     *dirId = pb.dInfo.dirInfo.ioDrDirID;
  150.     
  151.     return err;
  152. } // Path_To_DirId
  153.  
  154.  
  155.  
  156. /*******************************************************************************
  157.  * DirId_To_Path
  158.  *
  159.  *******************************************************************************/
  160.  
  161. short DirId_To_Path(short vRefNum, long dirId, Str32 rootVolName, Str255 pathToDir)
  162. {
  163.     Str32            dirName;
  164.     CInfoPBRec        dInfo;
  165.     OSErr            err;
  166.     
  167.     rootVolName[0] = '\0';
  168.     pathToDir[0] = '\0';
  169.     
  170.     dInfo.dirInfo.ioNamePtr = dirName;
  171.     dInfo.dirInfo.ioDrParID = dirId;
  172.     do
  173.     {
  174.         dInfo.dirInfo.ioVRefNum = vRefNum;
  175.         dInfo.dirInfo.ioFDirIndex = -1;
  176.         dInfo.dirInfo.ioDrDirID = dInfo.dirInfo.ioDrParID;
  177.         err = PBGetCatInfo(&dInfo, FALSE);
  178.         if (err)
  179.             return err;
  180.         if (dInfo.dirInfo.ioDrDirID == fsRtDirID)
  181.             BlockMove((Ptr)dirName, (Ptr)rootVolName, dirName[0] + 1);
  182.         else
  183.             Prepend_Dir_To_Path(dirName, pathToDir, pathToDir);
  184.     } while (dInfo.dirInfo.ioDrDirID != fsRtDirID);
  185.     return noErr;
  186. } // DirId_To_Path
  187.  
  188.  
  189.  
  190. /*******************************************************************************
  191.  * Get_SystemFolder_Path
  192.  *
  193.  *******************************************************************************/
  194.  
  195. short Get_SystemFolder_Path(Str32 rootVolName, Str255 pathToDir)
  196. {
  197.     SysEnvRec        theSysEnv;
  198.     OSErr            err;
  199.  
  200.     err = SysEnvirons(1, &theSysEnv);
  201.     if (err != noErr)
  202.         return err;
  203.     return DirId_To_Path(theSysEnv.sysVRefNum, 0L, rootVolName, pathToDir);
  204. } // Get_SystemFolder_Path
  205.  
  206.  
  207.  
  208. /*******************************************************************************
  209.  * Get_Preferences_Folder
  210.  *
  211.  *******************************************************************************/
  212.  
  213. short Get_Preferences_Folder(short *vRefNum, long *dirId)
  214. {
  215.     SysEnvRec        theSysEnv;
  216.     OSErr            err;
  217.  
  218.     err = SysEnvirons(1, &theSysEnv);
  219.     if (err != noErr)
  220.         return err;
  221.     if ((theSysEnv.systemVersion < 0x0700) || (FindFolder(kOnSystemDisk, 'pref' , TRUE, vRefNum, dirId) != noErr))
  222.     {
  223.         *vRefNum = theSysEnv.sysVRefNum;
  224.         *dirId = 0L;
  225.     }
  226.     return noErr;
  227. } // Get_Preferences_Folder
  228.  
  229.  
  230.  
  231. /*******************************************************************************
  232.  * Find_File1
  233.  *
  234.  *******************************************************************************/
  235.  
  236. short Find_File1(CInfoPBRec *fInfo, Str32 findFile, long searchDirId, short searchIndex,
  237.                 short searchDepth, long *foundDirId, short *foundIndex, Boolean findDirs,
  238.                 Boolean findFiles, Boolean *foundDirFlag, Boolean *foundFileFlag)
  239. {
  240.     OSErr        err;
  241.     
  242.     do
  243.     {
  244.         fInfo->hFileInfo.ioFDirIndex = searchIndex;
  245.         fInfo->hFileInfo.ioDirID = searchDirId;
  246.         err = PBGetCatInfo(fInfo, FALSE);
  247.         if (err == noErr)
  248.         {
  249.             *foundDirFlag = (((fInfo->hFileInfo.ioFlAttrib >> 4) & 0x01) == 1);
  250.             *foundFileFlag = !(*foundDirFlag);
  251.             if ((((findDirs) && (*foundDirFlag)) || ((findFiles) && (*foundFileFlag))) &&
  252.                 (RelString(fInfo->hFileInfo.ioNamePtr, findFile, FALSE, FALSE) == 0))
  253.             {
  254.                 if (*foundDirFlag)
  255.                     *foundDirId = fInfo->hFileInfo.ioDirID;
  256.                 else
  257.                     *foundDirId = fInfo->hFileInfo.ioFlParID;
  258.                 *foundIndex = searchIndex;
  259.                 return noErr;
  260.             }
  261.             if ((*foundDirFlag) && (searchDepth))
  262.             {
  263.                 if (Find_File1(fInfo, findFile, fInfo->hFileInfo.ioDirID, 1, searchDepth - 1, foundDirId,
  264.                         foundIndex, findDirs, findFiles, foundDirFlag, foundFileFlag) == noErr)
  265.                     return noErr;
  266.             }
  267.             searchIndex++;
  268.         }
  269.     } while (err == noErr);
  270.     
  271.     return err;
  272. } // Find_File1
  273.  
  274.  
  275.  
  276. /*******************************************************************************
  277.  * Find_File
  278.  *
  279.  *******************************************************************************/
  280.  
  281. short Find_File(Str32 findFile, short *vRefNum, long *dirId, short *index, short searchDepth,
  282.                 Boolean *findDirs, Boolean *findFiles)
  283. {
  284.     CInfoPBRec        fInfo;
  285.     Str255            fName;
  286.     
  287.     if (*vRefNum == 0)
  288.         Get_Root_Vol_Info(vRefNum, NULL);
  289.  
  290.     fInfo.hFileInfo.ioNamePtr = fName;
  291.     fInfo.hFileInfo.ioVRefNum = *vRefNum;
  292.     
  293.     return Find_File1(&fInfo, findFile, *dirId, *index, searchDepth, dirId, index, *findDirs,
  294.                         *findFiles, findDirs, findFiles);
  295. } // Find_File
  296.  
  297.  
  298.  
  299. /*******************************************************************************
  300.  * SFGetDirHook
  301.  *
  302.  *******************************************************************************/
  303.  
  304. pascal short SFGetDirHook(short item, DialogPtr theDialog)
  305. {
  306.     short        iType;
  307.     Handle        iHandle;
  308.     Rect        iRect;
  309.     
  310.     switch (item)
  311.     {
  312.         case 11:
  313.             return sfItemOpenButton;
  314.  
  315.         default:
  316.             return item;
  317.     }
  318. } // SFGetDirHook
  319.  
  320.  
  321.  
  322. /*******************************************************************************
  323.  * SFGetDirectory
  324.  *
  325.  *******************************************************************************/
  326.  
  327. Boolean SFGetDirectory(Point where, short dlogID, short *vRefNum, long *dirId)
  328. {
  329.     SFTypeList        typeList;
  330.     SFReply            reply;
  331.     
  332.     typeList[0] = '\0\0\0\0';
  333.     SFPGetFile(where, "\p", NULL, 1, typeList, (ProcPtr)SFGetDirHook, &reply, dlogID, NULL);
  334.     
  335.     *vRefNum = -(SFSaveDisk);
  336.     *dirId = CurDirStore;
  337.     
  338.     return reply.good;
  339. } // SFGetDirectory
  340.  
  341.  
  342.  
  343.